<?php
/**
 *问答社区能量豆积分控制器
 */
class Commwapbeans extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('commwap_model');
    }

    public function getBeansrankByQid()
    {
        $post = $this->input->post();
        if (!isset($post['qid'])||!is_numeric($post['qid'])) $this->error('error: lack question id');
        else $qid = (int)$post['qid'];

        if (!isset($post['pagesize'])||!is_numeric($post['pagesize'])) $this->error('error: lack pagesize');
        else $limit = (int)$post['pagesize'];

        if (isset($post['page'])&&is_numeric($post['page'])) $start = ((int)$post['page']-1)*$limit;
        else $start = 0;

        $end = $start + $limit -1;

        $res = $this->commwap_model->getBeansrankByQid($qid,$start,$end,$limit);
        $this->success($res);
    }

    public function getQuestionrank()
    {
        $post = $this->input->post();

        if (!isset($post['pagesize'])||!is_numeric($post['pagesize'])) $this->error('error: lack pagesize');
        else $limit = (int)$post['pagesize'];

        if (isset($post['page'])&&is_numeric($post['page'])) $start = ((int)$post['page']-1)*$limit;
        else $start = 0;

        $end = $start + $limit -1;

        $res = $this->commwap_model->getQuestionrank($start,$end,$limit);
        $this->success($res);
    }

    public function getQResponsed()
    {
        $post = $this->input->post();

        if (!isset($post['pagesize'])||!is_numeric($post['pagesize'])) $this->error('error: lack pagesize');
        else $limit = (int)$post['pagesize'];

        if (isset($post['page'])&&is_numeric($post['page'])) $offset = ((int)$post['page']-1)*$limit;
        else $offset = 0;

        $res = $this->commwap_model->getQResponsed($offset,$limit);
        $this->success($res);
    }

    public function dechargebeans()
    {
        $uid = $this->input->input_stream('uid');
        $num = $this->input->input_stream('num');
        $qid = $this->input->input_stream('qid');
        if (!isset($uid)||!is_numeric($uid)) $this->error('error: lack user id');
        else $uid = (int)$uid;
        if (!isset($num)||!is_numeric($num)) $this->error('error: invalid num');
        else $num = (int)$num;
        if (!isset($qid)||!is_numeric($qid)) $this->error('error: lack question id');
        else $qid = (int)$qid;
        $res = $this->commwap_model->decharge($uid,$qid,$num);
        if ($res === false) $this->error('out of max beans');
        $qbeans = $res['qbeans'];
        $res['qbeans']= strlen((string)$qbeans)>3?round((float)$qbeans/1000,1).'K':(string)$qbeans;
        $contributer = $res['contributer'];
        $res['contributer'] = strlen((string)$contributer)>3?round((float)$contributer/1000,1).'K':(string)$contributer;
        $res['dechargenum'] = $num;
        $this->success($res);
    }

    public function chargebeans()
    {
        $uid = $this->input->input_stream('uid');
        $num = $this->input->input_stream('num');
        if (!isset($uid)||!is_numeric($uid)) $this->error('error: lack user id');
        else $uid = (int)$uid;
        if (!isset($num)||!is_numeric($num)) $this->error('error: invalid num');
        else $num = (int)$num;
        $res = $this->commwap_model->charge($uid,$num);
        $res['chargenum'] = $num;
        $this->success($res);
    }
}